package cn.edu.sgu.www.mhxysy.service.jingmai.impl;

import cn.edu.sgu.www.mhxysy.dto.jingmai.JingmaiPointDTO;
import cn.edu.sgu.www.mhxysy.entity.jingmai.JingmaiPoint;
import cn.edu.sgu.www.mhxysy.entity.jingmai.StarStone;
import cn.edu.sgu.www.mhxysy.enums.InlayStatus;
import cn.edu.sgu.www.mhxysy.mapper.jingmai.JingmaiPointMapper;
import cn.edu.sgu.www.mhxysy.mapper.jingmai.StarStoneMapper;
import cn.edu.sgu.www.mhxysy.service.jingmai.JingmaiPointService;
import cn.edu.sgu.www.mhxysy.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class JingmaiPointServiceImpl implements JingmaiPointService {

    private final StarStoneMapper starStoneMapper;
    private final JingmaiPointMapper jingmaiPointMapper;

    @Autowired
    public JingmaiPointServiceImpl(StarStoneMapper starStoneMapper, JingmaiPointMapper jingmaiPointMapper) {
        this.starStoneMapper = starStoneMapper;
        this.jingmaiPointMapper = jingmaiPointMapper;
    }

    @Override
    public void insert(JingmaiPointDTO jingmaiPointDTO) {
        JingmaiPoint jingmaiPoint = toEntity(jingmaiPointDTO);

        jingmaiPoint.setId(null);

        jingmaiPointMapper.insert(jingmaiPoint);
    }

    @Override
    public void inlayStone(String stoneId, Integer jingmaiId) {
        // 1、修改星辰晶石的镶嵌状态
        UpdateWrapper<StarStone> updateWrapper = new UpdateWrapper<>();

        updateWrapper.eq("id", stoneId);
        updateWrapper.set("inlay_status", InlayStatus.YXQ.getValue());

        starStoneMapper.update(updateWrapper.getEntity(), updateWrapper);

        // 2、修改角色经脉
        JingmaiPoint jingmaiPoint = new JingmaiPoint();

        jingmaiPoint.setId(jingmaiId);
        jingmaiPoint.setStoneId(stoneId);

        jingmaiPointMapper.updateById(jingmaiPoint);
    }

    @Override
    public List<JingmaiPoint> selectByRoleId(String roleId) {
        QueryWrapper<JingmaiPoint> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq(
                StringUtils.isNotEmpty(roleId),
                "role_id", roleId
        );

        return jingmaiPointMapper.selectList(queryWrapper);
    }

    private JingmaiPoint toEntity(JingmaiPointDTO jingmaiPointDTO) {
        JingmaiPoint jingmaiPoint = new JingmaiPoint();

        jingmaiPoint.setId(jingmaiPointDTO.getId());
        jingmaiPoint.setRoleId(jingmaiPointDTO.getRoleId());
        jingmaiPoint.setStoneId(jingmaiPointDTO.getStoneId());
        jingmaiPoint.setJingmaiId(jingmaiPointDTO.getJingmaiId());

        return jingmaiPoint;
    }

}